from rest_framework import serializers
from .models import Workflow, WorkflowNode, WorkflowInstance, WorkflowHistory, User

class WorkflowNodeSerializer(serializers.ModelSerializer):
    handlers = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
    
    class Meta:
        model = WorkflowNode
        fields = ['id', 'name', 'node_type', 'next_nodes', 'timeout', 'handlers', 'condition_expression']

class WorkflowSerializer(serializers.ModelSerializer):
    nodes = WorkflowNodeSerializer(many=True, read_only=True)
    
    class Meta:
        model = Workflow
        fields = ['id', 'name', 'description', 'nodes', 'created_at', 'updated_at']

class WorkflowInstanceSerializer(serializers.ModelSerializer):
    histories = WorkflowHistorySerializer(many=True, read_only=True)
    
    class Meta:
        model = WorkflowInstance
        fields = ['id', 'workflow', 'current_node', 'status', 'created_by', 'created_at', 'histories']

class WorkflowHistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkflowHistory
        fields = ['id', 'node', 'action_user', 'action_time', 'action_data', 'comment'] 